<template>
  <div id="app">
    <h2>测试题</h2>
    <SubjectItem v-for="(item, index) in subjects" :key="index" :item="item" :index="index" @submit="checkAnswer" />
    <div>
      <FlagItem :results="results" />
    </div>
  </div>
</template>

<script>
import FlagItem from './components/FlagItem.vue';
import SubjectItem from './components/SubjectItem.vue';


export default {
  components: {
    FlagItem,
    SubjectItem
  },
  data() {
    return {
      subjects: this.generateSubjects(),
      results: Array(5).fill("未完成")
    };
  },
  methods: {
    generateSubjects() {
      let subjects = [];
      for (let i = 0; i < 5; i++) {
        const num1 = Math.floor(Math.random() * 10) + 1;
        const num2 = Math.floor(Math.random() * 10) + 1;
        subjects.push({ num1, num2, answer: num1 + num2 });
      }
      return subjects;
    },
    checkAnswer(index, userAnswer) {
      // 检查是否输入了答案
      if (userAnswer === undefined || userAnswer === null || userAnswer === '') {
        this.$set(this.results, index, "未完成");
      } else if (userAnswer === this.subjects[index].answer) {
        this.$set(this.results, index, "回答正确");
      } else {
        this.$set(this.results, index, "回答错误");
      }
      console.log("当前的答案状态：", this.results);
    },
  }
};
</script>

<style>
body {
  background-color: #eee;
}

#app {
  background-color: #fff;
  width: 500px;
  margin: 50px auto;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
  padding: 2em;
}
</style>